<?php
/**
 * Menus model
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Content Module
 * @subpackage Model
 * @license  New BSD License
 *
 */
class Models_Content_Menus extends Bel_Model_Abstract {
	
	
	/**
	 * Defines one-to-many relations between menus table and table that links menus and pages.  
	 *
	 * @var array
	 */
	protected $_dependentTables = array ('Models_Content_Page2menu' );
	
	/**
     * Constructor to set name of a table with menus in a database and table's primary key.
     * 
     * @param  mixed $config Array of user-specified config options - ZendFramework feature.
     * @return void
     */
	public function __construct($array = array()) {
		$this->_name = TABLE_PREFIX . 'menus';
		$this->_primary = array ('menu_id' );
		parent::__construct ( $array );
	}

	/**
	 * Returns all menus with pages assigned to them
	 *
	 * @param string $where - string with additional where clause
	 * @return array - with all menus and pages assigned to each menu
	 */
	public function getMenusWithPages($where = NULL) {		
		$rowset = $this->fetchAll ( $where );		
		
		$p2m = new Models_Content_Page2menu();
		$menus = array();
		$j = 0;
		foreach ( $rowset as $row ) {
			$menus [$j] = $row->toArray();
			$menus [$j]['pages'] = $row->findManyToManyRowset('Models_Content_Pages','Models_Content_Page2menu',null, null, $p2m->select()->order('menu_order'))->toArray();
			$j ++;
		}
		return $menus;
	}

	/**
	 * Returns menu with $system_name and pages assigned to this menu
	 *
	 * @param string $system_name - menu system name
	 * @return array
	 */
	public function getMenuBySystemName($system_name){
		return $this->getMenusWithPages(array('menu_system_name = ?' => $system_name));
	}
}